from utils.threads import RenewAllThread, CountThread, AddThread, PrintPatientListThread


# 调度器
class Scheduler(object):
    def __init__(self):
        self.thread_list = []
        self.window = None
        self.terminate = False

    def start(self, window, fn_start, fn_success, fn_error):
        self.window = window
        self.terminate = False
        t = CountThread(self, window)
        t.success.connect(window.update_status_msg)
        t.finish.connect(window.event_reset_click)
        t.start()

        for idx in range(window.table_view.model().rowCount()):
            pid = window.table_view.model().index(idx, 1).data()

            thread = RenewAllThread(self, idx, pid, window)
            thread.start_signal.connect(fn_start)
            thread.success.connect(fn_success)
            thread.error.connect(fn_error)
            # 
            # 线程开始
            thread.start()
            self.thread_list.append(thread)

    def stop(self):
        pass

    def destroy_thread(self, thread):
        self.thread_list.remove(thread)


SCHEDULER = Scheduler()  # 单例模式


class Adder(object):
    def __init__(self):
        self.thread_list = []
        self.window = None
        self.terminate = False

    def start(self, window, pid_list, fn_success, fn_error,):
        self.window = window
        self.terminate = False
        t = CountThread(self, window)
        t.success.connect(window.update_status_msg)
        t.start()

        for pid in pid_list:
            
            thread = AddThread(self, pid, window)
            thread.success.connect(fn_success)
            thread.error.connect(fn_error)
            # 
            # 线程开始
            thread.start()
            self.thread_list.append(thread)
           
    def stop(self):
        pass

    def destroy_thread(self, thread):
        self.thread_list.remove(thread)


ADDER = Adder()


class PrintPatientList():
    def __init__(self):
        self.thread_list = []
        self.window = None
        self.terminate = False

    def start(self, window, pid_list, fn_success, fn_error):
        self.window = window
        self.terminate = False
        t = CountThread(self, window)
        t.success.connect(window.update_status_msg)
        t.start()

        for pid in pid_list:
            thread = PrintPatientListThread(self, pid, window)
            thread.success.connect(fn_success)
            thread.error.connect(fn_error)
            #
            # 线程开始
            thread.start()
            self.thread_list.append(thread)

    def stop(self):
        pass

    def destroy_thread(self, thread):
        self.thread_list.remove(thread)


PRINTPATIENTLIST = PrintPatientList()
